package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/15 0015 8:28
 * description:管理员用户维护接口
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    /**
     * 管理员用户分页查询
     *
     * @param page     当前页
     * @param limit    每页显示条数
     * @param username 查询条件根据用户名查询
     * @param sort     排序字段
     * @param order    排序方式
     */
    @RequestMapping("/list")
    public Object list(Integer page, Integer limit, String username, String sort, String order) {

        IPage<DtsAdmin> pageResult = adminService.findByPage(page, limit, username, sort, order);

        //封装返回的数据
        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResult.getTotal());
        data.put("items", pageResult.getRecords());
        return ResponseUtil.ok(data);
    }

    //管理员添加
    @RequestMapping("create")
    public Object create(@RequestBody DtsAdmin dtsAdmin){
        dtsAdmin = adminService.insertAdmin(dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);

    }

    //管理员删除
    @RequestMapping("delete")
    public Object delete(int id){
        int rows = adminService.removeAdmin(id);
        return ResponseUtil.ok();
    }

    //修改管理员信息
    @RequestMapping("update")
    public Object update(@RequestBody DtsAdmin dtsAdmin){
        int rows = adminService.updateAdmin(dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);
    }


}